/**
 * @format
 * @Author: TC
 * @Date: 2021-09-28 15:21:52
 * @Last Modified by: TC
 * @Last Modified time: 2025-05-05 01:36:07
 */

import { _decorator, Button, Toggle, v3 } from "cc";
import { app } from "db://assets/FrameWork/App";
import { mFrameStorageConst } from "db://assets/FrameWork/Const/FrameStorageConst";
import { BaseUI } from "db://assets/FrameWork/Module/Component/BaseUI";
const { ccclass, property } = _decorator;

@ccclass("SettingUI")
export class SettingUI extends BaseUI {
    @property(Toggle)
    toggleMusic: Toggle = null;
    @property(Toggle)
    toggleEffect: Toggle = null;
    @property({ type: Button, displayName: "关闭按钮" })
    btnClose: Button = null;

    init() {
        super.init();
        this.updateUI();
    }

    protected addUIEvent(): void {
        super.addUIEvent();
        this.addClickEvent(this.btnClose, this.onClose);
    }

    private updateUI(): void {
        this.toggleMusic.isChecked = 1 == app.storageMgr.getNumber(mFrameStorageConst.MUSIC_VAL);
        this.toggleEffect.isChecked = 1 == app.storageMgr.getNumber(mFrameStorageConst.EFFECT_VAL);
        this.updateBtn();
    }

    onMusic(e: Toggle): void {
        if (this.toggleMusic.isChecked) {
            app.storageMgr.setNumber(mFrameStorageConst.MUSIC_VAL, 1);
        } else {
            app.storageMgr.setNumber(mFrameStorageConst.MUSIC_VAL, 0);
        }
        app.audioMgr.setMusicVal(app.storageMgr.getNumber(mFrameStorageConst.MUSIC_VAL));
        this.updateBtn();
    }

    onEffect(e: Toggle): void {
        if (this.toggleEffect.isChecked) {
            app.storageMgr.setNumber(mFrameStorageConst.EFFECT_VAL, 1);
        } else {
            app.storageMgr.setNumber(mFrameStorageConst.EFFECT_VAL, 0);
        }
        app.audioMgr.setEffectVal(app.storageMgr.getNumber(mFrameStorageConst.EFFECT_VAL));
        this.updateBtn();
    }

    private updateBtn(): void {
        if (this.toggleMusic.isChecked) {
            this.toggleMusic.node.getChildByName("toggleBtn").position = v3(55, 0, 0);
        } else {
            this.toggleMusic.node.getChildByName("toggleBtn").position = v3(-55, 0, 0);
        }
        if (this.toggleEffect.isChecked) {
            this.toggleEffect.node.getChildByName("toggleBtn").position = v3(55, 0, 0);
        } else {
            this.toggleEffect.node.getChildByName("toggleBtn").position = v3(-55, 0, 0);
        }
    }

    /**
     * 点击关闭
     */
    onClose(): void {
        this.closeUI();
    }
}
